from flask import render_template, request, redirect, url_for, Blueprint
from flask_login import login_required, current_user
from models import db

# 创建主蓝图
main_bp = Blueprint('main', __name__)

@main_bp.route('/')
def index():
    """首页重定向"""
    if current_user.is_authenticated:
        if current_user.role == 'admin':
            return redirect(url_for('main.admin_index'))
        else:
            return redirect(url_for('main.operator_index'))
    else:
        return redirect(url_for('auth.login'))

@main_bp.route('/admin')
@login_required
def admin_index():
    """管理员首页"""
    if current_user.role != 'admin':
        return redirect(url_for('main.operator_index'))
    
    return render_template('admin/index.html')

@main_bp.route('/operator')
@login_required
def operator_index():
    """操作员首页"""
    if current_user.role != 'operator':
        return redirect(url_for('main.admin_index'))
    
    return render_template('operator/index.html')

@main_bp.route('/admin/products')
@login_required
def admin_products():
    """管理员商品管理页面"""
    if current_user.role != 'admin':
        return redirect(url_for('main.operator_index'))
    
    return render_template('admin/products.html')

@main_bp.route('/admin/suppliers')
@login_required
def admin_suppliers():
    """管理员供应商管理页面"""
    if current_user.role != 'admin':
        return redirect(url_for('main.operator_index'))
    
    return render_template('admin/suppliers.html')

@main_bp.route('/admin/categories')
@login_required
def admin_categories():
    """管理员分类管理页面"""
    if current_user.role != 'admin':
        return redirect(url_for('main.operator_index'))
    
    return render_template('admin/categories.html')

@main_bp.route('/admin/users')
@login_required
def admin_users():
    """管理员操作员管理页面"""
    if current_user.role != 'admin':
        return redirect(url_for('main.operator_index'))
    
    return render_template('admin/users.html')

@main_bp.route('/admin/inventory')
@login_required
def admin_inventory():
    """管理员库存管理页面"""
    if current_user.role != 'admin':
        return redirect(url_for('main.operator_index'))
    
    return render_template('admin/inventory.html')

@main_bp.route('/admin/stock-in')
@login_required
def admin_stock_in():
    """管理员入库管理页面"""
    if current_user.role != 'admin':
        return redirect(url_for('main.operator_index'))
    
    return render_template('admin/stock-in.html')

@main_bp.route('/admin/stock-out')
@login_required
def admin_stock_out():
    """管理员出库管理页面"""
    if current_user.role != 'admin':
        return redirect(url_for('main.operator_index'))
    
    return render_template('admin/stock-out.html')

@main_bp.route('/admin/statistics')
@login_required
def admin_statistics():
    """管理员统计报表页面"""
    if current_user.role != 'admin':
        return redirect(url_for('main.operator_index'))
    
    return render_template('admin/statistics.html')

@main_bp.route('/operator/inventory')
@login_required
def operator_inventory():
    """操作员库存查看页面"""
    if current_user.role != 'operator':
        return redirect(url_for('main.admin_index'))
    
    return render_template('operator/inventory.html')

@main_bp.route('/operator/stock-in')
@login_required
def operator_stock_in():
    """操作员入库操作页面"""
    if current_user.role != 'operator':
        return redirect(url_for('main.admin_index'))
    
    return render_template('operator/stock-in.html')

@main_bp.route('/operator/stock-out')
@login_required
def operator_stock_out():
    """操作员出库操作页面"""
    if current_user.role != 'operator':
        return redirect(url_for('main.admin_index'))
    
    return render_template('operator/stock-out.html')

@main_bp.route('/admin/profile')
@login_required
def admin_profile():
    """管理员个人中心页面"""
    if current_user.role != 'admin':
        return redirect(url_for('main.operator_index'))
    
    return render_template('admin/profile.html')

@main_bp.route('/operator/profile')
@login_required
def operator_profile():
    """操作员个人中心页面"""
    if current_user.role != 'operator':
        return redirect(url_for('main.admin_index'))
    
    return render_template('operator/profile.html')


